<?php

use Biz\service\wikiRanking\Ranking;
use Doraemon\service\statSign\Basic as basicStatSign;

/**
 * 好物频道头部信息 - 好物榜单
 * Class ChannelHeadAction
 */
class RankingListAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;
    private $data = [];

    public function main()
    {
        $this->getRankingList();
        $this->getRankingInfoLink();
        $this->data = $this->data ? $this->data : (object)[];
        $this->setSuccess($this->data);
    }


    private function getRankingList()
    {
        $rankingList = Ranking::getRecommendRanking($this->uid);
        if (!empty($rankingList)) {
            foreach ($rankingList as $row) {
                $rankingId = $row['ranking_id'];
                $item = [
                    'type' => \Doraemon\tools\BlockType::TYPE_RANKING,
                    'ranking' => [
                        'ranking_info' => [
                            'ranking_id' => $rankingId,
                            'ranking_name' => $row['ranking_name'],
                            'background_color' => Ranking::getCacheData($rankingId),
                            'link' => Ranking::getRankingUrl($rankingId, Ranking::TYPE_HOT),
                            'ranking_list' => $this->handleRankingList($row['ranking_list'] ?? []),
                        ],
                    ],
                    'statSign' => basicStatSign::genStatSign(basicStatSign::MODULE_FINE_GOODS_RANKING, [
                        basicStatSign::EXTEND_KEY_OBJ_ID => $rankingId, basicStatSign::EXTEND_KEY_OBJ_TYPE => 'hot'], ['ranking_id' => $rankingId, 'ranking_type' => 'hot'])
                ];
                $this->data['ranking_list'][] = $item;
            }
        }
    }

    private function getRankingInfoLink()
    {
        $this->data['more_ranking_info_link'] = Ranking::getRankingUrl(0, 1, 0);
    }

    private function handleRankingList($list): array
    {
        $result = [];
        if ($list) {
            foreach ($list as $v) {
                $res['top'] = $v['top'] ?? '';
                $res['cover_img'] = $v['cover_img'] ?? '';
                $res['title'] = $v['title'] ?? '';
                $res['brand_name'] = $v['brand_name'] ?? '';
                $result [] = $res;
            }
        }
        return $result;
    }

}